/**
 * 获取cookie数据 注意：仅在浏览器环境可运行
 * @param {String} name
 * @returns {Object|String} 返回一个cookie组成的Object对象,如果name传值了就返回其name对应的值
 */
module.exports = function get(name) {
  if (typeof window === 'undefined') return {};
  const cookieStr = window.document.cookie;
  if (!cookieStr) return {};
  const cookieArr = cookieStr.split(';');
  const obj = {};
  for (const item of cookieArr) {
    const arr = item.split('=');
    obj[decodeURIComponent(arr[0].trim())] = decodeURIComponent(arr[1].trim());
  }
  return name ? obj[name] || null : obj;
};
